package com.zzh.service.impl;

import com.zzh.dao.CourseMapper;
import com.zzh.domain.Course;
import com.zzh.domain.CourseSection;
import com.zzh.domain.CourseVO;
import com.zzh.domain.Teacher;
import com.zzh.service.CourseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> findCourseByCondition(CourseVO courseVo) {
        return courseMapper.findCourseByCondition(courseVo);
    }

    @Override
    public void saveCourseOrTeacher(CourseVO courseVO) {
        try {
            Course course = new Course();
            BeanUtils.copyProperties(course, courseVO);
            Date date = new Date();
            course.setCreateTime(date);
            course.setUpdateTime(date);
            courseMapper.saveCourse(course);
            int id = course.getId();
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher, courseVO);
            teacher.setCourseId(id);
            teacher.setCreateTime(date);
            teacher.setUpdateTime(date);
            courseMapper.saveTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CourseVO findCourseById(int id) {
        return courseMapper.findCourseById(id);
    }

    @Override
    public void updateCourseOrTeacher(CourseVO courseVO) {
        try {
            Course course = new Course();
            BeanUtils.copyProperties(course, courseVO);
            Date date = new Date();
            course.setUpdateTime(date);
            courseMapper.updateCourse(course);
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher, courseVO);
            teacher.setCourseId(course.getId());
            teacher.setUpdateTime(date);
            courseMapper.updateTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateCourseStatus(int id, int status) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(status);
        course.setUpdateTime(new Date());
        courseMapper.updateCourseStatus(course);
    }
}
